package com.yanqu.road.server.cmd.activity.uniongroup;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionActivityGroupProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_UNION_ACTIVITY_GROUP_GET_USER_INFO_FROM_CROSS, desc = "拿玩家基础数据")
public class GetUserBaseInfoFromCrossCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage yanQuMessage) throws Exception {

        UnionActivityGroupProto.CrossGetUserBaseInfoReqMsg reqMsg = UnionActivityGroupProto.CrossGetUserBaseInfoReqMsg.parseFrom(yanQuMessage.getBytes());

        UnionActivityGroupProto.CrossGetUserBaseInfoRespMsg.Builder respMsg = UnionActivityGroupProto.CrossGetUserBaseInfoRespMsg.newBuilder();
        long serverId = GameServer.getInstance().getServerId();
        for (long userId : reqMsg.getUserIdList()) {
            UserBaseInfo userBaseInfo = UserMgr.getUserBaseInfo(userId, serverId);
            if(userBaseInfo != null){
                UnionActivityGroupProto.CrossUserBaseInfoTemp.Builder builder = UnionActivityGroupProto.CrossUserBaseInfoTemp.newBuilder();
                builder.setUserId(userId);
                builder.setBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                respMsg.addUser(builder);
            }
        }

        int activityId = reqMsg.getActivityId();
        int groupId = reqMsg.getGroupId();
        respMsg.setServerId(ConfigHelper.getLong("serverId"));
        GamePlayerMgr.sendUnionActivityGroupPacket(Protocol.C2_UNION_ACTIVITY_GET_USER_INFO_BACK, respMsg, 0, activityId, groupId);
        GameUnionActivityGroupMgr.getLogger().info("fetch unionWar user info");
    }

}
